﻿using Newtonsoft.Json;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.ViewModels
{
    /// <summary>
    /// 用来取号和叫号
    /// </summary>
    public class 取号叫号系统:BaseViewModel
    {
        /// <summary>
        /// 锁
        /// </summary>
        private readonly object m_LockRequest = new object();

        /// <summary>
        /// 名称
        /// </summary>
        [Reactive] public string Name { get; set; } = "";

        /// <summary>
        /// 当前执行编号
        /// </summary>
        private int m_CurrenttNum = 0;

        /// <summary>
        /// 当前的叫号
        /// </summary>
        [JsonIgnore]
        public int CurrenttNum
        {
            get
            {
                return m_CurrenttNum;
            }
            set
            {
                if (m_CurrenttNum == value)
                {
                    return;
                }

                int old = m_CurrenttNum;
                m_CurrenttNum = value;
                this.RaisePropertyChanged();
                //跳过已经被报废了的号
                lock (m_LockRequest)
                {
                    if (ScrapNums.Contains(old))
                    {
                        ScrapNums.Remove(old);
                    }

                    if (ScrapNums.Contains(m_CurrenttNum))
                    {
                        ScrapNums.Remove(m_CurrenttNum);
                        CurrenttNum++;
                    }
                }
            }
        }

        /// <summary>
        /// 报废的号
        /// </summary>
        [JsonIgnore]
        public List<int> ScrapNums { get; set; } = new List<int>();



        /// <summary>
        /// 当前排队的号码(取号)
        /// </summary>
        [JsonIgnore]
        [Reactive]
        public int CurrentRequestNum { get; set; }


        /// <summary>
        /// 获取当前正在叫的号码
        /// </summary>
        /// <returns></returns>
        public int GetCurrenttNum()
        {
            lock (m_LockRequest)
            {
                //移除报废的号码
                while (true)
                {
                    int old = m_CurrenttNum;
                    if (ScrapNums.Contains(old))
                    {
                        CurrenttNum++;
                        ScrapNums.Remove(old);
                        continue;
                    }
                    break;
                }

                return m_CurrenttNum;
            }
        }


        /// <summary>
        /// 取号(取出当前号之后)
        /// </summary>
        /// <returns></returns>
        public int RequestNum()
        {
            lock (m_LockRequest)
            {
                int current = CurrentRequestNum;
                CurrentRequestNum = CurrentRequestNum + 1;
                return current;
            }
        }

        /// <summary>
        /// 报废(号码,报废之后号码自动延顺)
        /// </summary>
        /// <param name="num"></param>
        public void Scrap(int num)
        {
            if (num == -1)
            {
                return;
            }
            lock (m_LockRequest)
            {
                if (!ScrapNums.Contains(num))
                {
                    ScrapNums.Add(num);
                }
                if (CurrenttNum == num)
                {
                    //直接报废掉
                    CurrenttNum++;
                }
                //else
                //{
                //    if (!ScrapNums.Contains(num))
                //    {
                //        ScrapNums.Add(num);
                //    }
                //}
            }
        }

        public static 取号叫号系统 Create()
        {
            取号叫号系统 取号叫号系统=new 取号叫号系统() {Name=$"取号叫号{ProductLine.Instance.RequestEntities.Count+1}" };
            return 取号叫号系统;
        }
    }
}
